package com.svs.framework.util;

import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.cloopen.rest.sdk.CCPRestSmsSDK;

public class SmsRequestApi {

	// *沙盒环境（用于应用开发调试）：restAPI.init("sandboxapp.cloopen.com", "8883");*
	// *生产环境（用户应用上线使用）：restAPI.init("app.cloopen.com", "8883"); *

	//现骨干账号信息

	static String serverIP = "app.cloopen.com";
	static String serverPort = "8883";
	static String accountSid = "8a216da8567745c001567d70fdcf0692";
	// AUTH TOKEN。
	static String accountToken = "0403a1a31b19444fab31ab8ee1553806";
	//static String accountToken = "0403a1a31b19444fab31ab8ee1553806";

	static String appId = "8a216da87051c90f017056f3d1d102bf";

	static CCPRestSmsSDK restAPI = null;

	//注册验证码短信通知模板ID
	public static String register_template_id = "562887"  ; //您好！您的验证码是{1}，请于5分钟内正确输入
	
	//找回密码
	public static String passwd_template_id = "488749"  ; //您好！您的验证码是{1}，请于5分钟内正确输入
	
	//企业审核通过
	public static String enter_pass_template_id = "488748"  ; //您好！您的验证码是{1}，请于5分钟内正确输入
	
	//寻呼通知短信模板ID
	public static String paging_template_id = "351152" ; //您收到一条未读寻呼，请及时登录系统查看。
	
	private static SmsRequestApi api;
	
	static Logger logger = LoggerFactory.getLogger(SmsRequestApi.class) ;

	private SmsRequestApi() {
		init();
	}

	public synchronized static SmsRequestApi getApi() {
		if (null == api) {
			api = new SmsRequestApi();
		}
		return api;
	}

	private void init() {
		restAPI = new CCPRestSmsSDK();
		restAPI.init(serverIP, serverPort);
		restAPI.setAccount(accountSid, accountToken);
		restAPI.setAppId(appId);
	}
	
	
	public static void main(String[] args) {
		accountSid = "8a216da8567745c001567d70fdcf0692";
		accountToken = "0403a1a31b19444fab31ab8ee1553806";
		appId = "8a216da87051c90f017056f3d1d102bf";
		register_template_id = "562887";
		restAPI = new CCPRestSmsSDK();
		restAPI.init(serverIP, serverPort);
		restAPI.setAccount(accountSid, accountToken);
		restAPI.setAppId(appId);
		String[] datas = new String[] { "123456" };
		sendSms("15267094681", SmsRequestApi.register_template_id, datas);
	}
	
	
	/**
	 * 带模板ID
	 * 
	 * @param phone
	 * @param datas
	 * @return
	 */

	public static boolean sendSms(String phone, String[] datas,String templateId) {
		if (null == restAPI) {
			api.init();
		}
		Map<String, Object> result = restAPI.sendTemplateSMS(phone, templateId, datas);
		logger.info("发送短信结果=============="+result.toString());
		if ("000000".equals(result.get("statusCode"))) {
			return true;
		}
		return false;
	}
	

	/**
	 * 发送短信
	 * 
	 * @param phone
	 * @param templateId
	 * @param datas
	 * @return
	 */

	public static boolean sendSms(String phone, String templateId, String[] datas) {
		if (null == restAPI) {
			api.init();
		}
		Map<String, Object> result = restAPI.sendTemplateSMS(phone, templateId, datas);
		if ("000000".equals(result.get("statusCode"))) {
			return true;
		}
		return false;
	}



}
